var fs = require('fs');
var ejs = require('ejs');
var path = require('path');
var url = require('url');
var querystring = require('querystring');

module.exports = function(req,res) {
    req.pcp = function (callback) {
        var post = '';
        
        req.on('data', function(chunk){
            post += chunk;
        });
        req.on('end', function() {
            var postData = querystring.parse(post);
            callback(postData);
        });
    }

    req.gcp = function(){
        var urlObj = url.parse(req.url, true);
        return urlObj.query;
    }

    res.render = function(viewPath, viewData) {
        fs.readFile(viewPath, 'utf8', function(err,data){
            if(err){
                console.log(err);
            }else{
                var html_data = ejs.render(data, viewData);
                res.end(html_data);
            }
        });
    }

    res.redirect = function(location){
        res.writeHead(302,{
            'Location':location
        });
        res.end();
    }
}